#pragma once
#include "protocal.hpp"
#include <iostream>
using namespace std;

class serverCal
{
public:
    response calculateHalder(request &req)
    {
        response resp(0, 0);
        switch (req.op)
        {
        case '+':
            resp.result_ = req.x + req.y;
            break;
        case '-':
            resp.result_ = req.x - req.y;
            break;
        case '*':
            resp.result_ = req.x * req.y;
            break;
        case '/':
        {
            if (req.y == 0)
                resp.code_ = 1;
            else
            {
                resp.result_ = req.x / req.y;
            }
        }
        break;

        case '%':
        {
            if (req.y == 0)
                resp.code_ = 2;
            else
            {
                resp.result_ = req.x % req.y;
            }
        }
        break;
        default:
            break;
        }
        return resp;
    }

    string calculate(string &package)
    {
        string info;
        bool r = Decode(package, info);
        if (!r)
            return " ";
        request req;
        r = req.Deserialize(info);
        if (!r)
            return " ";
        response resp = calculateHalder(req);
        string newinfo;
        resp.Serialize(newinfo);
        newinfo = Encode(newinfo);
        return newinfo;
    }
};